<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="Invoice ID">
              <a-input placeholder="" v-model="queryParam.id"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">Search</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">Reset</a-button>
              <!--<a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>-->
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">New</a-button>
     <!-- <a-button type="primary" icon="download" @click="handleExportXls('invoice_info')">导出</a-button>
      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>-->
      <!-- 高级查询区域 -->
<!--
      <j-super-query :fieldList="superFieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query>
-->
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
     <!-- <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>-->

<!--
      :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
      bordered
-->
      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        class="j-table-force-nowrap"
        :rowClassName="getRowClassname"
        @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a  @click="handleEdit(record)" v-if="record.status !== 2" >Edit</a>
          <a  @click="handleInvoiceDetail(record)" v-if="record.status === 2" >Details</a>

          <a-divider type="vertical" v-if="record.status !== 2" />
          <a-dropdown v-if="record.status !== 2">
            <a class="ant-dropdown-link">More <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <!--<a-menu-item>
                <a @click="handleDetail(record)">Details</a>
              </a-menu-item>-->
              <a-menu-item>
                <a @click="handleSendEmail(record)">Send Email</a>
              </a-menu-item>

             <a-menu-item>
                <a @click="handlePreviewPdf(record)">Preview PDF</a>
              </a-menu-item>

               <a-menu-item>
                <a @click="handleInvoicePdf(record)">Download PDF</a>
              </a-menu-item>

               <a-menu-item>
                 <a-popconfirm title="Make sure that the payment has been completed?" @confirm="() => handCompleted(record.id)">
                  <a>Payment completed</a>
                </a-popconfirm>
              <!--  <a @click="handleDetail(record)">Payment completed</a>-->
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="Confirm to Delete?" @confirm="() => handleDelete(record.id)">
                  <a>Delete</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>

      </a-table>
    </div>

    <invoice-info-modal ref="modalForm" @ok="modalFormOk"></invoice-info-modal>
    <invoice-detail-modal ref="detailForm" ></invoice-detail-modal>
    <invoice-send-email-modal ref="sendEmailModal" @ok="modalFormOk" ></invoice-send-email-modal>
    <invoice-down-pdf-modal ref="downpdfModal" ></invoice-down-pdf-modal>
    <pdf-preview-modal ref="pdfmodal"></pdf-preview-modal >

  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import InvoiceInfoModal from './modules/InvoiceInfoModal'
  import InvoiceDetailModal from './modules/InvoiceDetailModal'
  import InvoiceSendEmailModal from './modules/InvoiceSendEmailModal'
  import InvoiceDownPdfModal from './modules/InvoiceDownPdfModal'
  import PdfPreviewModal from '../../jeecg/modules/PdfPreviewModal'
  import Vue from 'vue'
  import {downFile, getAction, httpAction} from "@api/manage";
  import {ACCESS_TOKEN} from "@/store/mutation-types";

  export default {
    name: 'InvoiceInfoList',
    mixins:[JeecgListMixin, mixinDevice],
    components: {
      InvoiceInfoModal,InvoiceDetailModal,InvoiceSendEmailModal,InvoiceDownPdfModal,PdfPreviewModal
    },
    data () {
      return {
        description: 'invoice_info',
        // 表头
        columns: [
          /*{
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },*/
          {
            title:'Invoice Id',
            align:"center",
            dataIndex: 'id',
            width:100,
            sorter: true
          },
          {
            title:'First Name',
            align:"center",
            dataIndex: 'firstName',
            width:120,
            sorter: true
          },
          {
            title:'Last Name',
            align:"center",
            dataIndex: 'lastName',
            width:120,
            sorter: true

          },
          {
            title:'Amount',
            align:"center",
            dataIndex: 'invoiceAmountFormat',
            width:160,
            customRender:(text,record,index) => {
               return text;
            }
          },
          {
            title:'Status',
            align:"center",
            dataIndex: 'status',
            width:80,
            customRender:(text,record,index) => {
              if(text === '2' || text === 2){
                return  <span>Paid</span>
              }else{
                return  <span>Reserved</span>
              }
            }

          },
          {
            title:'Date',
            align:"center",
            dataIndex: 'createTime',
            width:150,
            customRender:function (text) {
              return !text?"":(text.length>10?text.substr(0,10):text)
            }
          },
          {
            title:'Sending Status',
            align:"center",
            dataIndex: 'invoiceStatus',
            width:80,
            customRender:function (text) {
              if(text === '10' || text === 10){
                return  <span>Sent</span>
              }else{
                return  <span>Unsent</span>
              }
            }
          },
          {
            title:'Last Sent Time',
            align:"center",
            dataIndex: 'invoiceTime',
            width:150,
            customRender:function (text) {
              return text;
            }
          },
          {
            title:'Payment Due Date',
            align:"center",
            dataIndex: 'lastPayTime',
            width:150,
            customRender:function (text) {
              return !text?"":(text.length>10?text.substr(0,10):text)
            }
          },
          {
            title: '',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: "/sistersophie/invoiceInfo/list",
          delete: "/sistersophie/invoiceInfo/delete",
          deleteBatch: "/sistersophie/invoiceInfo/deleteBatch",
          exportXlsUrl: "/sistersophie/invoiceInfo/exportXls",
          importExcelUrl: "sistersophie/invoiceInfo/importExcel",
          paymentCompleted: "/sistersophie/invoiceInfo/paymentCompleted",
          downLoadPdf: "/sistersophie/invoiceInfo/downloadPdf",
          sendEmail: "/sistersophie/invoiceInfo/sendEmail",
        },
        dictOptions:{},
        superFieldList:[],
      }
    },
    created() {
    this.getSuperFieldList();
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      handleSendEmail(record){
        this.$refs.sendEmailModal.edit(record);
        this.$refs.sendEmailModal.title="Send Email";
        this.$refs.sendEmailModal.disableSubmit = false;
      },
      handlePreviewPdf(record){
        const token = Vue.ls.get(ACCESS_TOKEN);
        this.headers = {"X-Access-Token":token}
        this.$refs.pdfmodal.previewFiles(record.id+"",token);
      },
      handleInvoicePdf(record){
        this.$refs.downpdfModal.edit(record);
        this.$refs.downpdfModal.title="Down Pdf";
        this.$refs.downpdfModal.disableSubmit = true;
      },
      handleInvoiceDetail:function(record){
        this.$refs.detailForm.edit(record);
        this.$refs.detailForm.title="Detail";
        this.$refs.detailForm.disableSubmit = true;
      },
      handCompleted:function(id){
        let that = this;
        let httpurl = this.url.paymentCompleted;
        let method = 'post';
        let params = {};
        params.id = id;
        httpAction(httpurl,params,method).then((res)=>{
          if(res.success){
            that.$message.success(res.message);
            this.loadData(1);
           }else{
            that.$message.warning(res.message);
          }
        }).finally(() => {
        })
      },
      initDictConfig(){
      },
      getRowClassname(record){
        if(record.payWarningFlag === 1 ){
          return "data-rule-invalid"
        }
      },
      getSuperFieldList(){
        let fieldList=[];
        fieldList.push({type:'int',value:'studentId',text:'学生ID',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'amount',text:'应付金额',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'taxamount',text:'税收金额',dictCode:''})
        fieldList.push({type:'double',value:'discount',text:'折扣百分比',dictCode:''})
        fieldList.push({type:'double',value:'credit',text:'抵扣余额',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'invoiceAmount',text:'付款金额',dictCode:''})
        fieldList.push({type:'string',value:'remarks',text:'备注信息',dictCode:''})
        fieldList.push({type:'int',value:'status',text:'状态，0=未付款，1=付款中，2=已付款，3=付款失败',dictCode:''})
        fieldList.push({type:'int',value:'isDelete',text:'删除标志 0=未删除，1=已删除',dictCode:''})
        fieldList.push({type:'string',value:'createName',text:'创建人',dictCode:''})
        fieldList.push({type:'string',value:'updateName',text:'更新人',dictCode:''})
        fieldList.push({type:'int',value:'invoiceStatus',text:'通知状态 0=未通知，10=已通知',dictCode:''})
        fieldList.push({type:'date',value:'invoiceTime',text:'通知时间'})
        this.superFieldList = fieldList
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';

  /deep/ .data-rule-invalid{
    background: #f4f4f4;
    color: #bababa;
  }
</style>